package com.kzd.btgame.custom

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.kzd.btgame.R
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg

class MultiStatusLayout : ConstraintLayout {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    private val mLoadingHint: View by lazy {
        createViewEmptyNull(context, color = R.color.white).apply {
            setOnClickListener { }
        }
    }

    private val mLoadingView: View by lazy {
        createViewEmptyLoad(context, color = R.color.white).apply {
            setOnClickListener { }
        }
    }

    fun showLoadingHint(hint: String? = null) {
        hideLoadingView()
        val loadingHint = mLoadingHint.apply {
            findViewById<TextView>(R.id.tvEmptyNullHint).text = hint
        }
        addView(loadingHint)

    }

    fun hideLoadingHint() {
        removeView(mLoadingHint)
    }

    fun showLoadingView() {
        hideLoadingHint()
        removeView(mLoadingView)
        addView(mLoadingView)
    }

    fun hideLoadingView() {
        removeView(mLoadingView)
    }

    fun hideLoading() {
        hideLoadingView()
        hideLoadingHint()
    }

    var isStatusSuccess: Boolean = false

    fun setLoadingStatus(response: HttpResponseBody<*>) {
        when (if (isStatusSuccess) return else response.status) {
            Status.START -> {
                showLoadingView()
            }
            Status.FAILURE -> {
                showLoadingHint(hint = response.error?.msg)
            }
            Status.SUCCESS -> {
                hideLoadingView()
                isStatusSuccess = true
            }
        }
    }
}